<?php
include_once "header.php";


if(isset($_GET["id_peng"])){
	$daftar_pengiriman_nonfisik = new Daftar_pengiriman_nonfisik();
	$pengiriman = $daftar_pengiriman_nonfisik->getById($_GET["id_peng"]);
	

}

$cek_field="";

if (ISSET($_POST["ubah_pengiriman_nonfisik"])){


	if($_POST["jenis_pengiriman"]=="" || $_POST["tujuan"]=="" || $_POST["tgl"]==""){
		$cek_field="Peringatan: Silahkan isikan seluruh data yang tersedia.";
		$daftar_pengiriman_nonfisik = new Daftar_pengiriman_nonfisik();
		$pengiriman = $daftar_pengiriman_nonfisik->getById($_POST["id_peng"]);
		
	
	} else {
		$daftar_pengiriman_nonfisik = new Daftar_pengiriman_nonfisik();
		$pengiriman = $daftar_pengiriman_nonfisik->getById($_POST["id_peng"]);
		
		$daftar_sk = new Daftar_surat_keluar();
		$sk = $daftar_sk->getById($_POST['id']);
		//var_dump($sk);
		$daftar_jenis = new Daftar_jenis();
		$jenis = $daftar_jenis->getById($sk->getJenis());
		
		$nomor = ucwords($jenis->getJenis())."-".$sk->getNomor();
		$tgl = $sk->getTgl();
		$random_digit=rand(0000,99999);
	
		$main_folder="images/bukti_kirim/";
		$file=$_FILES["image"]["name"];
		
		$waktu=$_POST['tgl']." ".$_POST['jam'].":".$_POST['mnt'].":".$_POST['dtk'];
			$pengiriman_nonfisik = new Pengiriman_nonfisik();
			$pengiriman_nonfisik->setId($_POST['id_peng']);
			$pengiriman_nonfisik->setKdSK($_POST['id']);
			$pengiriman_nonfisik->setWaktuKirim($waktu);
			$pengiriman_nonfisik->setJenisPengiriman($_POST['jenis_pengiriman']);
			$pengiriman_nonfisik->setTujuan($_POST['tujuan']);
			
		if($file==""){
			
			$pengiriman_nonfisik->setBuktiKirim($pengiriman->getBuktiKirim());
			$daftar_pengiriman_nonfisik->edit($pengiriman_nonfisik);
			header("Location:detil_sk.php?id=".$sk->getId()."");
		
		}
		$tipeFile=$_FILES["image"]["type"];
		$eks = explode(".", $file);
		$ekstensi = array_pop($eks);
		$filename = $nomor."_".$tgl."_".$random_digit.".".$ekstensi;
		
		
		$file_upload = $main_folder . $filename;
		
		
		if (move_uploaded_file($_FILES["image"]["tmp_name"],$file_upload)){
			$pengiriman_nonfisik->setBuktiKirim($filename);
			$daftar_pengiriman_nonfisik->edit($pengiriman_nonfisik);
			header("Location:detil_sk.php?id=".$sk->getId()."");
		}

	
	}
}
?>

<div id="container">
	<div id="errorcont">
		<?php echo $cek_field;
		?>
	</div>
	<div id="maincont">
		<?php include_once "menu_samping.php"; ?>
		<div id="mainright">
			<div id="title">
			Ubah Pengiriman Nonfisik
			</div>
			<div id="body">
			</br>
			<form name="form1" method="POST" enctype="multipart/form-data" action="ubah_pengiriman_nonfisik.php">
			<input type="hidden" name="ubah_pengiriman_nonfisik">
			<input type="hidden" name="id_peng" value="<?php echo $pengiriman->GetId(); ?>">
			<input type="hidden" name="id" value="<?php echo $pengiriman->GetKdSK(); ?>">
			<table border=0 cellspacing=0 cellpadding=4 width=100%>		
				<tr>
					<td>Jenis Pengiriman</td>
					<td>:</td>
					<td>
					<select name="jenis_pengiriman">
						<?php
						if($pengiriman->GetKdSK()=="email"){
							$select="selected";
						} else {$select="";}
						
						if($pengiriman->GetKdSK()=="faks"){
							$select="selected";
						} else {$select="";}
						?>
						<option value="email" <?php echo $select; ?>>Email</option>
						<option value="faks" <?php echo $select; ?>>Faks</option>
					
					<select>
					</td>
				</tr>
				
				<tr>
					<td>Tujuan</td>
					<td>:</td>
					<td>
					<input type="text" name="tujuan" value="<?php echo $pengiriman->GetTujuan(); ?>"> 
				</tr>
				
				<tr>
					<td>Waktu</td>
					<td>:</td>
					<td>
					<input type="text" name="tgl" id="tgl" value="<?php echo date('Y-m-d', strtotime($pengiriman->GetWaktuKirim()));?>">
					
					<select name="jam"> 
					<option value="">Jam</option>
					<?php for($i=0; $i<=24; $i++){
					if($i==date('H', strtotime($pengiriman->GetWaktuKirim()))){
						echo "<option value=\"".$i."\" selected>".$i."</option>"; 
					} else {
						echo "<option value=\"".$i."\">".$i."</option>"; 
					}
					} ?>
					</select>
					
					<select name="mnt"> 
					<option value="">Menit</option>
					<?php for($i=0; $i<=60; $i++){
					if($i==date('s', strtotime($pengiriman->GetWaktuKirim()))){
						echo "<option value=\"".$i."\" selected>".$i."</option>"; 
					} else {
						echo "<option value=\"".$i."\">".$i."</option>"; 
					}
					} ?>
					</select>
					
					<select name="dtk"> 
					<option value="">Detik</option>
					<?php for($i=0; $i<=60; $i++){
					if($i==date('i', strtotime($pengiriman->GetWaktuKirim()))){
						echo "<option value=\"".$i."\" selected>".$i."</option>"; 
					} else {
						echo "<option value=\"".$i."\">".$i."</option>"; 
					}
					} ?>
					</select>
					
					</td>
				</tr>
	
			
				<tr>
					<td>Bukti kirim</td>
					<td>:</td>
					<td>
					<input type="file" name="image" value="<?php echo $pengiriman->getBuktiKirim(); ?>" size=20>
					</td>
				</tr>
				<tr>
					<td></td>
					<td></td>
					<td align="right">
					
					<input type="submit" value="Simpan"> 
					<input type="button" value="Batal" onClick="window.location.href='detil_sk.php?id=<?php echo $pengiriman->getKdSK(); ?>'">
					
					</td>
				</tr>
	
			</table>
			<br>
			
			</form>
						
			</div>
        </div>
	</div>
</div>
<?php
	include_once "footer.php";
?>

<script type="text/javascript">
	$(function() { 
	$("#tgl").datepicker({dateFormat: "yy-mm-dd",
											buttonImage:'jquery/css/images/calendar.gif',
											buttonImageOnly: true,
											showOn: 'button'
	}); 
	});
	
</script>

